<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="22">
            <el-form-item label="楼层名称" prop="name"
                :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22">
            <el-form-item label="楼层排序" prop="sort"
                :rules="[
                  {required: true, message:'楼层排序不能为空', trigger:'blur'},
                  {validator: validator.isDigits, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写楼层排序"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22">
            <el-form-item label="实际高度" prop="height"
                :rules="[
                 ]">
              <el-input v-model="inputForm.height" placeholder="请填写实际高度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="楼层包含" prop="type"
            :rules="[
             ]">
            <el-checkbox-group
                v-model="floorTypeList">
               <el-checkbox v-for="item in $dictUtils.getDictList('floor_type')" :label="item.label" :key="item.id">{{item.label}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="楼层地图" prop="mapFile"
                :rules="[
                 ]">
              <map-upload
                v-model = "inputForm.mapFile"
                @preview="previewMap"
              >
              </map-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="蓝牙地图" prop="ibeaconMap"
                :rules="[
                 ]">
              	<map-upload
                v-model = "inputForm.ibeaconMap"
                @preview="previewMap"
              >
              </map-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="VR地图" prop="vrMap"
                :rules="[
                 ]">
              <map-upload
                v-model = "inputForm.vrMap"
                @preview="previewMap"
              >
              </map-upload>
          </el-form-item>
        </el-col>
        <el-col :span="22" v-if="hasPermission('smis:system')">
          <el-form-item label="楼层音频" prop="audio"
                :rules="[
                 ]">
            	<el-upload-ext
              :limit = "1"
              accept = ".mp3"
              fileType = "1"
              :allowOtherSize = "false"
              :allowScale = "true"
              v-model = "inputForm.audio"
            >
            </el-upload-ext>
          </el-form-item>
        </el-col>
        <el-col :span="22">
            <el-form-item label="相机距离" prop="singleCameraDis"
                :rules="[
                 ]">
              <el-input v-model="inputForm.singleCameraDis" placeholder="请填写相机距离"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22">
            <el-form-item label="中心X" prop="centerX"
                :rules="[
                 ]">
              <el-input v-model="inputForm.centerX" placeholder="请填写中心X"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22">
            <el-form-item label="中心Y" prop="centerY"
                :rules="[
                 ]">
              <el-input v-model="inputForm.centerY" placeholder="请填写中心Y"     ></el-input>
           </el-form-item>
        </el-col>

        <el-col :span="22">
            <el-form-item label="旋转角度" prop="singleRotateAngle"
                :rules="[
                 ]">
              <el-input v-model="inputForm.singleRotateAngle" placeholder="请填写旋转角度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22">
            <el-form-item label="单层仰角" prop="singleTiltAngle"
                :rules="[
                 ]">
              <el-input v-model="inputForm.singleTiltAngle" placeholder="请填写单层仰角"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22" v-if="hasPermission('smis:system')">
            <el-form-item label="定位系统ID" prop="aoaId"
                :rules="[
                 ]">
              <el-input v-model="inputForm.aoaId" placeholder="请填写定位系统"     ></el-input>
           </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    <map-preview ref="mapPreview"></map-preview>
  </el-dialog>
</div>
</template>

<script>
  import MapPreview from '@/components/jfMap/preview.vue'
  import MapUpload from '@/components/jfMap/mapUpload.vue'
  import ElUploadExt from '@/components/elUploadExt'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          sort: '',
          mapFile: '',
          ibeaconMap: '',
          vrMap: '',
          aoaId: '',
          building: {
            id: '',
            longitude: '',	// 经度
            latitude: ''	// 纬度
          },
          type: '',
          audio: '',
          height: '',
          road: '',
          centerX: '',
          centerY: '',
          singleCameraDis: '',
          singleRotateAngle: '',
          singleTiltAngle: ''
        },
        floorTypeList: []
      }
    },
    components: {
      MapPreview,
      MapUpload,
      ElUploadExt
    },
    methods: {
      init (method, id, searchForm) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建楼层`
        } else if (method === 'edit') {
          this.title = '修改楼层'
        } else if (method === 'view') {
          this.title = '查看楼层'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.height = 0;
          this.inputForm.centerX = 0;
          this.inputForm.centerY = 0;
          this.inputForm.singleCameraDis = 20000;
          this.inputForm.singleRotateAngle = 0;
          this.inputForm.singleTiltAngle = 65;

          this.floorTypeList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/floor/jfFloor/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfFloor)
              if(this.inputForm.type && this.inputForm.type.length > 1) {
                this.floorTypeList = this.inputForm.type.split(",")
              }
              this.loading = false
            })
          } else {
            this.inputForm.building =  searchForm.building
          }
        })
      },
      previewMap (mapUrl) {
        this.$refs.mapPreview.init(mapUrl, this.inputForm.building)
      },
      // 表单提交
      doSubmit () {
        this.inputForm.type = this.floorTypeList.join(',')
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/floor/jfFloor/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
